<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Think;
class UserSession {
	
	function execute()
	{
		$context = Think::instance('Think\Context');
		//session_start();
// 		if($_GET['sessid']) $_COOKIE['PHPSESSID'] = $_GET['sessid'];
		
        //不使用 GET/POST 变量方式
//         ini_set('session.use_trans_sid',    0);
        //设置垃圾回收最大生存时间
        ini_set('session.gc_maxlifetime',   3600);
        //使用 COOKIE 保存 SESSION ID 的方式
        ini_set('session.use_cookies',      1);
        ini_set('session.cookie_path',      '/');
        //多主机共享保存 SESSION ID 的 COOKIE
        ini_set('session.cookie_domain',$this->getRootDomain());
//         session_start();

// 		$verify_str = 'HTTP_USER_AGENT:'.getenv( 'HTTP_USER_AGENT');  //UA
// 		if(!$_SESSION['verify_str']) 
// 			$_SESSION['verify_str'] = $verify_str;
// 		elseif($_SESSION['verify_str'] && $verify_str != $_SESSION['verify_str'])
// 		{
// 			$old_id = session_id();
// 			session_regenerate_id();
// 			session_unset();
// 		}
		if(isset($_SESSION['otoUserID'])){
			$context->userId = $_SESSION['otoUserID'];
		}
	//	$context->userId = 22;
	}
	/**
	 * 取得域名根
	 */
	function getRootDomain()
	{
		$hostArray = explode('.', $_SERVER['HTTP_HOST']);
		$last = array_pop($hostArray);
		if(strpos($last,':')) 
			list($last,) = explode(':',$last);
		return '.'.array_pop($hostArray).'.'.$last;
	}
}
